﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebDKMH.Models;
using PagedList;

namespace WebDKMH.Controllers
{
    public class AdminController : Controller
    {
        //
        // GET: /Admin/
        dangkymonhocEntities2 db = new dangkymonhocEntities2();
         public ActionResult Index()
        {
            return View();
        }
        public ActionResult thongtincanhan_admin()
        {
            string smasv = Session["LoginUserID"].ToString();
            var ttad=( from tt in db.sinhviens
                       where tt.masv==smasv
                     select tt).ToList();
                         
            return View(ttad);
        }
        public ActionResult doimatkhau_admin()
        {
            return View();
        }
        public ActionResult thongkedkmh()
        {
            
            return View();
        }
        public ActionResult kqdkmh()
        {
            return View();
        }
        public ActionResult thongbao()
        {
            var thongbao = (from tb in db.thongbao1
                            select tb).ToList();


            return View(thongbao);
        }
        [HttpPost]
        public ActionResult thongbao(FormCollection col, thongbao1 thongbao3)
        {
            //string kh = True;
            thongbao3.kichhoat = true;
            db.thongbao1.Add(thongbao3);
            db.SaveChanges();
            return RedirectToAction("thongbao");
        }
        #region[dangthongbao]
       
        public ActionResult dangthongbao(string type, int id1)
        {
            //int id2 = Convert.ToInt32(id1);
            if (type == "add")
                return View();
            else
            {
                var chitiet = db.thongbao1.First(m => m.id == id1);
                return View(chitiet);
            }
        }
        [ValidateInput(false)]
        [HttpPost]

        public ActionResult dangthongbao(FormCollection collection, thongbao1 thongbao2, string type, int id1)
        {
            //dangkymonhocEntities db = new dangkymonhocEntities();
            string smoi =collection["moi"];
            string sdkmh =collection["dkmh"];
            //bool bmoi = Convert.ToBoolean(smoi);
            //bool bdkmh = Convert.ToBoolean(sdkmh);
            string std = collection["txttieude"];
            string Detail = collection["txtDetail"];
            bool bmoi;
            if (smoi == "on")
            {
                bmoi = true;
            }
            else
            {
                bmoi = false;
            }
            bool bdkmh;
            if (sdkmh == "on")
            {
                bdkmh = true;
            }
            else
            {
                bdkmh= false;
            }

            if (type == "add")
            {

                thongbao2.@new = bmoi;
                thongbao2.tgdk = bdkmh;
                thongbao2.tieude = std;
                thongbao2.noidung = Detail;
                db.thongbao1.Add(thongbao2);
            }
            else
            {
                var a = db.thongbao1.First(m => m.id == id1);
                a.@new = bmoi;
                a.tgdk = bdkmh;
                a.tieude = std;
                a.noidung = Detail;
                UpdateModel(a);
            }
            db.SaveChanges();

            return RedirectToAction("thongbao");
            // return View();
        }
        #endregion
      
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Xoa(int id1)
        {
            thongbao1 TB = db.thongbao1.Single(n => n.id == id1);
            db.thongbao1.Remove(TB);
            db.SaveChanges();
            return RedirectToAction("thongbao");
            //return View();
        }
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult kichhoat(int id1, FormCollection col)
        {
            var n = db.thongbao1.First(m => m.id == id1);
            if (n.kichhoat == false || n.kichhoat == null)
            {
                n.kichhoat = true;
            }
            else
            {
                n.kichhoat = false;
            }
            UpdateModel(n);
            db.SaveChanges();


            return RedirectToAction("thongbao");
        }
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult moi(int id1, FormCollection col)
        {
            var n = db.thongbao1.First(m => m.id == id1);
            if (n.@new == false || n.@new == null)
            {
                n.@new = true;
            }
            else
            {
                n.@new = false;
            }
            UpdateModel(n);
            db.SaveChanges();


            return RedirectToAction("thongbao");
        }
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult tgdk(int id1, FormCollection col)
        {
            var n = db.thongbao1.First(m => m.id == id1);
            if (n.tgdk == false || n.tgdk == null)
            {
                n.tgdk = true;
            }
            else
            {
                n.tgdk = false;
            }
            UpdateModel(n);
            db.SaveChanges();


            return RedirectToAction("thongbao");
        }
        #region[thoigian]

        public ActionResult thoigian(string type, int id1)
        {
          
            // var ViewData =  new SelectList(db.lops.Select(r=>r.makhoa)).Distinct();
            //var ProjectQry = (from p in db.lops
            //                 select p).Distinct();
            //ViewData["Projects2"] = new SelectList(db.lops.Select(r => r.khoahoc).Distinct());
            //var Project = (from p in db.lops
            //               // orderby p.khoahoc

            //               select p);
            if (type == "add")
            {
               // ViewData["Projects1"] = new SelectList(db.lops.Select(r => r.makhoa).Distinct()) ;
               ViewData["Projects1"]= new SelectList(db.khoas.Select(r =>r.makhoa).Distinct());
                                      //from l in db.lops
                                      //from k in db.khoas
                                      //where l.makhoa == k.makhoa 
                                      //).Distinct(); 
                                       
                return View();
            }
            else
            {
                var chitiet = db.thoigiandks.First(m => m.id == id1);
                return View(chitiet);
            }
        }
        [ValidateInput(false)]
       
     [HttpPost]
        public ActionResult thoigian(FormCollection col, thoigiandk thoigian, string type, int id1)
        {
            string tk = col["Projects1"];
            DateTime tn =Convert.ToDateTime(col["txtDateTungay"]);
            DateTime dn = Convert.ToDateTime(col["txtDateDenngay"]);


            if (type == "add")
            {
                    thoigian.makhoa = tk;
                    thoigian.ngaybatdau = tn;
                    thoigian.ngayketthuc = dn;
                    db.thoigiandks.Add(thoigian);
            }
            else
            {
                var a = db.thoigiandks.First(m => m.id == id1);
                    //( from tg in db.thoigiandks
                //           from k in db.khoas
                //             where tg.makhoa==k.makhoa
                //             select tg.id==id1).First(); 
                    //db.thoigiandks.First(m => m.id == id1);
                a.ngaybatdau = tn;
                a.ngayketthuc = dn;
                UpdateModel(a);
            }
            db.SaveChanges();

                
            return RedirectToAction("xemthoigian");
        }
        
        public ActionResult xemthoigian()
        {
            var tg = (from t in db.thoigiandks select t);
            return View(tg);
        }
        // public object blankLine { get; set; }
        public ActionResult xoathoigian(int id1)
        {
            thoigiandk Tg = db.thoigiandks.Single(n => n.id == id1);
            db.thoigiandks.Remove(Tg);
            db.SaveChanges();
            return RedirectToAction("xemthoigian");
        }
        #endregion
        public ActionResult dssv(string sortOrder, string currentFilter, string searchString, int? page)
        {
            ViewBag.CurrentSort = sortOrder;
            ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
            ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";

            if (searchString != null)
            {
                page = 1;
            }
            else
            {
                searchString = currentFilter;
            }

            ViewBag.CurrentFilter = searchString;

            var students = from s in db.sinhviens
                           select s;

            if (!String.IsNullOrEmpty(searchString))
            {
                students = students.Where(s => s.makhoa.Contains(searchString)
                                       || s.malop.Contains(searchString));
            }
            switch (sortOrder)
            {
                case "name_desc":
                    students = students.OrderByDescending(s => s.masv);
                    break;
                case "Date":
                    students = students.OrderBy(s => s.ngaysinh);
                    break;
                case "date_desc":
                    students = students.OrderByDescending(s => s.ngaysinh);
                    break;
                default:  // Name ascending 
                    students = students.OrderBy(s => s.masv);
                    break;
            }
            int pageSize = 100;
            int pageNumber = (page ?? 1);

            var DeptList = new List<string>();

            var DeptQuery = from q in db.sinhviens orderby q.makhoa select q.makhoa;

            var abc = from h in db.sinhviens orderby h.malop select h.malop;

            DeptList.AddRange(DeptQuery.Distinct());
            DeptList.AddRange(abc.Distinct());

            ViewBag.searchString = new SelectList(DeptList);

            IList<sinhvien> empList = new List<sinhvien>();

            var emp = from q in db.sinhviens select q;
            if (!String.IsNullOrEmpty(searchString))
            {
                emp = emp.Where(s => s.makhoa == searchString);
            }

            var obc = from h in db.sinhviens select h;
            if (!String.IsNullOrEmpty(searchString))
            {
                obc = obc.Where(w => w.malop == searchString);
            }

            var EmpList = obc.ToList();
            foreach (var empData in EmpList)
            {
                empList.Add(new sinhvien()
                {
                    masv = empData.masv,
                    ho = empData.ho,
                    ten = empData.ten,
                    ns0 = empData.ns0,
                    gioitinh = empData.gioitinh,
                    malop = empData.malop,
                    khoahoc = empData.khoahoc,
                    noisinh = empData.noisinh,
                    makhoa = empData.makhoa,
                    email = empData.email
                });
            }
            var myEmpList = emp.ToList();
            foreach (var empData in myEmpList)
            {
                empList.Add(new sinhvien()
                {
                    masv = empData.masv,
                    ho = empData.ho,
                    ten = empData.ten,
                    ns0 = empData.ns0,
                    gioitinh = empData.gioitinh,
                    malop = empData.malop,
                    khoahoc = empData.khoahoc,
                    noisinh = empData.noisinh,
                    makhoa = empData.makhoa,
                    email = empData.email
                });
            }
            return View(empList.ToPagedList(pageNumber, pageSize));
        }
        public ActionResult suasv(string masv)
        {
           
            var a = db.sinhviens.First(u => u.masv == masv);
            return View(a);
           
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult suasv(string masv, FormCollection col)
        {
            var a = db.sinhviens.First(u => u.masv == masv);
            string Masv = col["Masv"];
            string Ho = col["Ho"];
            string Ten = col["Ten"];
            DateTime Ngaysinh = Convert.ToDateTime(col["birthday"]);
            string gioitinh = col["sex"];
            string Malop = col["Malop"];
            string Khoahoc = col["Khoahoc"];
            string Noisinh = col["Noisinh"];
            string makhoa = col["makhoa"];
            string email = col["email"];
            string matkhau = col["matkhau"];
            a.masv = Masv;
            a.ho = Ho;
            a.ten = Ten;
            //a.ngaysinh =Ngaysinh;
            a.gioitinh = gioitinh;
            a.malop = Malop;
            a.khoahoc = Khoahoc;
            a.noisinh = Noisinh;
            a.makhoa = makhoa;
            a.email = email;
            a.matkhau = matkhau;
            UpdateModel(a);
            db.SaveChanges();
            return RedirectToAction("dssv");
        }
        public ActionResult XoaSV(string masv)
        {

            sinhvien user = db.sinhviens.Single(n => n.masv == masv);
            db.sinhviens.Remove(user);
            db.SaveChanges();
            return RedirectToAction("dssv");
        }

        public ActionResult themsv()
        {
            return View();
        }
        [HttpPost]
        public ActionResult themsv(sinhvien a, FormCollection col)
        {
            string Masv = col["Masv"];
            string Ho = col["Ho"];
            string Ten = col["Ten"];
            DateTime Ngaysinh = Convert.ToDateTime(col["birthday"]);
            string gioitinh = col["sex"];
            string Malop = col["Malop"];
            string Khoahoc = col["Khoahoc"];
            string Noisinh = col["Noisinh"];
            string makhoa = col["makhoa"];
            string email = col["email"];
            string matkhau = col["matkhau"];

            a.masv = Masv;
            a.ho = Ho;
            a.ten = Ten;
            //us.ns0 = Ngaysinh;
            a.gioitinh = gioitinh;
            a.malop = Malop;
            a.khoahoc = Khoahoc;
            a.noisinh = Noisinh;
            a.makhoa = makhoa;
            a.email = email;
            a.matkhau = matkhau;

            UpdateModel(a);
            db.SaveChanges();
            return RedirectToAction("dssv");
        }
        public ViewResult dsgv(string sortOrder, string currentFilter, string searchString, int? page)
        {
            ViewBag.CurrentSort = sortOrder;
            ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
            var gvien = from s in db.giaoviens
                        select s;
            switch (sortOrder)
            {
                case "name_desc":
                    gvien = gvien.OrderByDescending(s => s.magv);
                    break;

                default:  // Name ascending 
                    gvien = gvien.OrderBy(s => s.magv);
                    break;
            }

            int pageSize = 100;
            int pageNumber = (page ?? 1);
            return View(gvien.ToPagedList(pageNumber, pageSize));
        }
        [HttpGet]
        public ViewResult dsgv(string sortOrder, int? page, string search = null)
        {
            ViewBag.CurrentSort = sortOrder;
            ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
            ViewBag.Search = search;
            IEnumerable<giaovien> gvien = db.giaoviens.Where(
            p => search == null
            || p.magv.Contains(search)
            || p.hotengv.Contains(search)
            || p.gvc.Contains(search));
            switch (sortOrder)
            {
                case "name_desc":
                    gvien = gvien.OrderByDescending(s => s.magv);
                    break;

                default:  // Name ascending 
                    gvien = gvien.OrderBy(s => s.magv);
                    break;
            }

            int pageSize = 100;
            int pageNumber = (page ?? 1);
            return View(gvien.ToPagedList(pageNumber, pageSize));
        }
        public ActionResult suagv(string magv)
        {

            var a = db.giaoviens.First(u => u.magv == magv);
            return View(a);

        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult suagv(string magv, FormCollection col)
        {
            var a = db.giaoviens.First(u => u.magv == magv);
            string Magv = col["Magv"];
             string GVC = col["gvc"];
            string Hoten = col["Hotengv"];
            a.magv = Magv;
            a.hotengv = Hoten;
            a.gvc =GVC;
           
            UpdateModel(a);
            db.SaveChanges();
            return RedirectToAction("dsgv");
        }
        public ActionResult Xoagv(string magv)
        {

           giaovien user = db.giaoviens.Single(n => n.magv == magv);
            db.giaoviens.Remove(user);
            db.SaveChanges();
            return RedirectToAction("dsgv");
        }



        public ActionResult themgv()
        {
            return View();
        }

        [HttpPost]
        public ActionResult themgv(giaovien a, FormCollection col)
        {
            string Magv = col["Masv"];
            string gvc = col["gvc"];
            string Hoten = col["Hoten"];

            //string Ten = col["Ten"];
            //DateTime Ngaysinh = Convert.ToDateTime(col["birthday"]);
            //string gioitinh = col["sex"];
            //string Malop = col["Malop"];
            //string Khoahoc = col["Khoahoc"];
            //string Noisinh = col["Noisinh"];
            //string makhoa = col["makhoa"];
            //string email = col["email"];
            //string matkhau = col["matkhau"];

            a.magv = Magv;
            a.hotengv = Hoten;
            a.gvc = gvc;
            //us.ns0 = Ngaysinh;
            //a.gioitinh = gioitinh;
            //a.malop = Malop;
            //a.khoahoc = Khoahoc;
            //a.noisinh = Noisinh;
            //a.makhoa = makhoa;
            //a.email = email;
            //a.matkhau = matkhau;

            UpdateModel(a);
            db.SaveChanges();
            return RedirectToAction("dsgv");
        }

        public ActionResult QuanliDT()
        {
            return View();
        }
    }
}

